#include <stdio.h>
#include <math.h>
#include <stdint.h>

#include "platform.h"

const unsigned short costab[] = {
//

    2048, 2049, 2051, 2053, 2055, 2057, 2059, 2061, 2063, 2065, 2066, 2068,
    2070, 2072, 2074, 2076, 2077, 2079, 2081, 2083, 2085, 2086, 2088, 2090,
    2092, 2093, 2095, 2097, 2098, 2100, 2102, 2103, 2105, 2106, 2108, 2109,
    2111, 2112, 2114, 2115, 2117, 2118, 2119, 2121, 2122, 2123, 2124, 2126,
    2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138,
    2138, 2139, 2140, 2141, 2141, 2142, 2143, 2143, 2144, 2144, 2145, 2145,
    2146, 2146, 2146, 2146, 2147, 2147, 2147, 2147, 2147, 2147, 2147, 2147,
    2147, 2147, 2147, 2147, 2147, 2147, 2146, 2146, 2146, 2146, 2145, 2145,
    2144, 2144, 2143, 2143, 2142, 2141, 2141, 2140, 2139, 2138, 2138, 2137,
    2136, 2135, 2134, 2133, 2132, 2131, 2130, 2129, 2128, 2127, 2126, 2124,
    2123, 2122, 2121, 2119, 2118, 2117, 2115, 2114, 2112, 2111, 2109, 2108,
    2106, 2105, 2103, 2102, 2100, 2098, 2097, 2095, 2093, 2092, 2090, 2088,
    2086, 2085, 2083, 2081, 2079, 2077, 2076, 2074, 2072, 2070, 2068, 2066,
    2065, 2063, 2061, 2059, 2057, 2055, 2053, 2051, 2049, 2048, 2046, 2044,
    2042, 2040, 2038, 2036, 2034, 2032, 2030, 2029, 2027, 2025, 2023, 2021,
    2019, 2018, 2016, 2014, 2012, 2010, 2009, 2007, 2005, 2003, 2002, 2000,
    1998, 1997, 1995, 1993, 1992, 1990, 1989, 1987, 1986, 1984, 1983, 1981,
    1980, 1978, 1977, 1976, 1974, 1973, 1972, 1971, 1969, 1968, 1967, 1966,
    1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1957, 1956, 1955,
    1954, 1954, 1953, 1952, 1952, 1951, 1951, 1950, 1950, 1949, 1949, 1949,
    1949, 1948, 1948, 1948, 1948, 1948, 1948, 1948, 1948, 1948, 1948, 1948,
    1948, 1948, 1948, 1949, 1949, 1949, 1949, 1950, 1950, 1951, 1951, 1952,
    1952, 1953, 1954, 1954, 1955, 1956, 1957, 1957, 1958, 1959, 1960, 1961,
    1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1971, 1972, 1973, 1974,
    1976, 1977, 1978, 1980, 1981, 1983, 1984, 1986, 1987, 1989, 1990, 1992,
    1993, 1995, 1997, 1998, 2000, 2002, 2003, 2005, 2007, 2009, 2010, 2012,
    2014, 2016, 2018, 2019, 2021, 2023, 2025, 2027, 2029, 2030, 2032, 2034,
    2036, 2038, 2040, 2042, 2044, 2046,

/*

 2048, 2113, 2177, 2239, 2298, 2352, 2401, 2444, 2481, 2509, 2530, 2543,
 2548, 2543, 2530, 2509, 2481, 2444, 2401, 2352, 2298, 2239, 2177, 2113,
 2048, 1982, 1918, 1856, 1798, 1743, 1694, 1651, 1614, 1586, 1565, 1552,
 1548, 1552, 1565, 1586, 1614, 1651, 1694, 1743, 1797, 1856, 1918, 1982,
 */

/*    2048, 2282, 2513, 2736, 2948, 3143, 3320, 3476, 3606, 3710, 3786, 3832,    //
 3848, 3832, 3786, 3710, 3606, 3476, 3320, 3143, 2948, 2736, 2513, 2282,    //
 2048, 1813, 1582, 1359, 1147, 952, 775, 619, 489, 385, 309, 263,    //
 248, 263, 309, 385, 489, 619, 775, 952, 1147, 1359, 1582, 1813    //*/
//
    };

static volatile int incr = 11;

static void modulate(int bit)
{
    incr = bit ? 6 : 11;
}

// 144.64M / 10957 = 13200Hz
void RF_UpdatePhase(void)
{
    static unsigned short phase;
//    incr = 6;
    phase += incr;
    phase %= 330;    // 200Hz, *6=1200Hz, *11=2200Hz
    dac_data_set(DAC_ALIGN_12B_R, costab[phase]);

    static int count = 0;
    count++;
    count %= 55;    // 1200Hz
    if(count == 0) {
        zaprs_poll();
        /*
         static int cnt=0;
         cnt++;
         cnt%=1200;
         if(cnt==0)printf("1200\n");
         */
    }
}

static void tx_start_f(void)
{
    timer_enable(TIMER13);
    timer_enable(TIMER5);
}
static void tx_symbol_f(char bit)
{
    //NRZI编码: 状态改变(0->1 or 1->0)代表"0", 状态不变代表"1"
    static unsigned char oldstat = 0;
    if(bit == 0)
//    { oldstat = oldstat ? 0 : 1; }
        {
        oldstat = !oldstat;
    }
    modulate(oldstat);
}

static void tx_char_f(int c)
{
    printf("%02X ", c & 0xff);
    fflush(stdout);
//    putchar(c);
}

static void tx_end_f(void)
{
//    timer_disable(TIMER13);
//    timer_disable(TIMER5);
}

void RF_Config(void)
{
    zaprs_cb_t cb;
    cb.tx_char_f = tx_char_f;
    cb.tx_end_f = tx_end_f;
    cb.tx_start_f = tx_start_f;
    cb.tx_symbol_f = tx_symbol_f;
//    zaprs_init(&cb, (const unsigned char*)"BH1PHL");
    zaprs_init(&cb, (const unsigned char*)"BH1QQN");
}
